package com.github.tvbox.osc.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.github.tvbox.osc.R
import com.github.tvbox.osc.base.BaseLazyFragment
import com.github.tvbox.osc.ui.activity.Media.MediaActionView
import com.github.tvbox.osc.ui.activity.Media.MediaBaseView
import com.github.tvbox.osc.ui.activity.Media.MediaPreview
import com.github.tvbox.osc.ui.activity.Media.MediaVodPanelView
import com.github.tvbox.osc.viewmodel.SourceViewModel
import com.orhanobut.logger.Logger
import io.github.source.media.bean.MediaBean
import io.github.source.media.bean.MediaResult

/**
 * @ClassName: DetailActivityFragment
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: huangrongcai
 * @date: 2025/6/15
 *
 * @Copyright: 黄嵘才（HuangRc)
 */
class DetailActivityFragment : BaseLazyFragment() {
    private val TAG = "APP.DetailActivityFragment"
    private var mSourceViewModel: SourceViewModel? = null
    private val mediaBean: MediaBean? = null

    //UI===
    private var mMediaPreview: MediaPreview? = null
    private var mMediaVodPanelView: MediaVodPanelView? = null
    private var mMediaBase: MediaBaseView? = null
    private var mMediaActionView: MediaActionView? = null

    private val searchList: List<MediaBean> = ArrayList()

    companion object {
        /**
         * ID
         */
        var VOD_ID = "vodId"

        /**
         * 源站
         */
        var SITE_KEY = "siteKey"

        /**
         * 名称（关键字等）
         */
        var QUERY_WORD = "queryWord"
        fun newInstance(vodId: String? = null, siteKey: String? = null): Fragment {
            val fragment = DetailActivityFragment()

            val bundle = Bundle()
            bundle.putString(VOD_ID, vodId)
            bundle.putString(SITE_KEY, siteKey)
            bundle.putString(QUERY_WORD, "")
            fragment.arguments = bundle

            return fragment
        }
    }

    override fun getLayoutResID(): Int {
        return R.layout.skin_fragment_detail
    }

    protected fun onNewIntent(intent: Intent?) {
        initData()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        EventBus.getDefault().register(this)
        setupBackEvent()
        initView(rootView)
        initViewModel()
        initData(arguments)
    }

    private fun initView(rootView: View?) {
        val rootView = findViewById<View>(R.id.skin_root_view)
        mMediaBase = MediaBaseView.create(this, rootView)
        mMediaPreview = MediaPreview.create(this, rootView)
        mMediaActionView = MediaActionView.create(rootView)
        mMediaVodPanelView = MediaVodPanelView.create(rootView)

        /*-----点播面板-------*/
    }

    /**
     * 解析入参
     */
    private fun initData(bundle: Bundle?) {
        val VOD_ID = bundle?.getString(VOD_ID, "")
        val SITE_KEY = bundle?.getString(SITE_KEY, "")
        val QUERY_WORD = bundle?.getString(QUERY_WORD, "")

        mSourceViewModel?.fetchDetails(SITE_KEY, VOD_ID)
    }

    private fun initViewModel() {
        mSourceViewModel = ViewModelProvider(this)[SourceViewModel::class.java]
        mSourceViewModel?.MediaSearchResult?.observe(
            viewLifecycleOwner
        ) { result: MediaResult<List<MediaBean?>?>? ->
            //
        }
        //媒体文件详情已获取
        mSourceViewModel?.MediaDetailResult?.observe(
            viewLifecycleOwner
        ) { result: MediaResult<List<MediaBean?>?>? ->
            //
            Logger.d("MediaDetailResult:$result")
            result?.data?.get(0)?.let {media->
                mMediaBase?.setMediaDetails(media)
                mMediaPreview?.setMediaDetails(media)
            }
        }
    }
    private fun setupBackEvent() {
        requireActivity().onBackPressedDispatcher.addCallback(
            viewLifecycleOwner,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    // 在这里处理 Fragment 的返回逻辑
                    if (shouldInterceptBack()) {
                        // 拦截返回事件
                        //
                    } else {
                        // 不拦截，交给系统处理
                        isEnabled = false
                        requireActivity().onBackPressed()
                    }
                }
            }

        )

    }

    fun shouldInterceptBack(): Boolean {
        // 根据你的需求判断是否需要拦截返回事件
        return mMediaPreview?.shouldInterceptBack() ?: return false
    }
}